package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mapper.UserMapper;
import com.example.model.dtos.PageResult;
import com.example.model.dtos.UserDTO;
import com.example.model.pojo.User;
import com.example.service.UserService;
import com.example.utils.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name_", username);

        //查询数据库中是否存在该用户
        User user = userMapper.selectOne(queryWrapper);
        if (user == null){
            return null;
        }

        //判断数据库中密码与传入密码是否一直
        String userPassword = user.getUserPassword();
        if (BCrypt.checkpw(password, userPassword)){
            return user;
        }

        return null;
    }

    @Override
    public Boolean addUser(String username, String password) {

        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_name_", username);

        User user = userMapper.selectOne(queryWrapper);
        if (user!=null){
            return false;
        }
        User addUser = new User();
        addUser.setUserName(username);
        addUser.setInWarehouse("0");
        addUser.setOutWarehouse("0");
        addUser.setGoods("0");
        addUser.setStatistics("0");
        addUser.setBasic("0");
        addUser.setSystem("0");


//        将密码加密
        String gensalt = BCrypt.gensalt();
        String addPassword = BCrypt.hashpw(password, gensalt);
        addUser.setUserPassword(addPassword);
        //存入数据库中
        userMapper.insert(addUser);

        return true;
    }

    @Override
    public Boolean updateUser(User user) {
        int i = userMapper.updateById(user);
        if (i==1){
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteUser(String id){
        int i = userMapper.deleteById(id);
        if (i==1){
            return true;
        }
        return false;
    }

    @Override
    public PageResult userList(Integer pageStart, Integer pageSize) {

        //创建分页对象并将当前页码和页面大小写入
        Page<User> userPage=new Page<>(pageStart, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        userMapper.selectPage(userPage, queryWrapper);
        List<User> records = userPage.getRecords();
        records.forEach(user -> user.setUserPassword(""));
//        返回一个分页后的结果
        PageResult pageResult = new PageResult(pageStart.longValue(), pageSize.longValue(), userPage.getTotal(),records);

        return pageResult;

    }
}
